CC=msp430-elf-gcc
CFLAGS=-Os -g -mmcu=msp430f2617 -Wall -Wpedantic -Werror -fpack-struct -std=c99 -Iinclude/ -I/opt/ti/mspgcc/include -Wl,-Map=out.map
FLASHER=tools/z1-bsl-nopic
#OBJECTS=cc2420.o z1_spi.o z1_uart.o z1_cc2420.o
BUILD=build
DRIVER_OBJECTS=uart.o dbg_uart.o cc2420.o spi.o
OBJECTS=main.o receive.o error.o $(foreach driver, $(DRIVER_OBJECTS), drivers/$(driver))
 
all: out.ihex

$(BUILD)/%.o: %.c | $(BUILD)
	mkdir -p $(@D)
	$(CC) $(CFLAGS) -c -o $@ $<

$(BUILD):
	mkdir -p $@

out.ihex: $(foreach obj, $(OBJECTS), $(BUILD)/$(obj))
	$(CC) $(CFLAGS) -o out.elf $^
	msp430-elf-objcopy --output-target=ihex out.elf $@

flash: out.ihex
	$(FLASHER) --z1 -c $(DEV) -r -e -I -p $<
	stty -F $(DEV) 115200 min 1 -parenb crtscts

reset:
	$(FLASHER) --z1 -c $(DEV) -r
	stty -F $(DEV) 115200 min 1 -parenb crtscts

clean:
	rm -R build/*
